package com.example.work.ui.notifications;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.work.DBService;
import com.example.work.Diary;
import com.example.work.Login;
import com.example.work.NoteEdit;
import com.example.work.R;
import com.example.work.register;

import java.text.SimpleDateFormat;
import java.util.Date;


public class NotificationsFragment extends Fragment {

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_notifications, container, false);
        return view;                   //相应的布局文件
    }
    public class NoteEdit extends AppCompatActivity {

        DBService myDb;
        private Button btnCancel;
        private Button btnSave;
        private EditText titleEditText;
        private EditText contentEditText;
        private TextView timeTextView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_note_edit);

            init();
            if(timeTextView.getText().length()==0)
                timeTextView.setText(getTime());
        }

        private void init() {

            myDb = new DBService(this);
            SQLiteDatabase db = myDb.getReadableDatabase();
            titleEditText = findViewById(R.id.et_title);
            contentEditText = findViewById(R.id.et_content);
            timeTextView = findViewById(R.id.edit_time);
            btnCancel = findViewById(R.id.btn_cancel);
            btnSave = findViewById(R.id.btn_save);

            //按钮点击事件
            btnCancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(NoteEdit.this,Diary.class);
                    startActivity(intent);
                }
            });
            btnSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    SQLiteDatabase db = myDb.getWritableDatabase();
                    ContentValues values = new ContentValues();

                    String title= titleEditText.getText().toString();
                    String content=contentEditText.getText().toString();
                    String time= timeTextView.getText().toString();

                    if("".equals(titleEditText.getText().toString())){
                        Toast.makeText(NoteEdit.this,"标题不能为空",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if("".equals(contentEditText.getText().toString())) {
                        Toast.makeText(NoteEdit.this,"内容不能为空",Toast.LENGTH_LONG).show();
                        return;
                    }
                    values.put(DBService.TITLE,title);
                    values.put(DBService.CONTENT,content);
                    values.put(DBService.TIME,time);
                    db.insert(DBService.TABLE,null,values);
                    Toast.makeText(NoteEdit.this,"保存成功",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(NoteEdit.this,Diary.class);
                    startActivity(intent);
                    db.close();
                }
            });
        }

        //获取当前时间
        private String getTime() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            String str = sdf.format(date);
            return str;
        }

    }
}

